#!/bin/bash
# vim: set ft=sh

set -e -x

if [ -z "$RESOURCE_NAME" ]; then
  echo "must specify \$RESOURCE_NAME" >&2
  exit 1
fi

gzip -c resource-image/rootfs.tar > ${RESOURCE_NAME}.tar.gz

git clone concourse bumped-concourse

set +x
echo "$BOSH_PRIVATE_CONFIG" > bumped-concourse/config/private.yml
set -x

cd bumped-concourse/

# work-around Go BOSH CLI trying to rename blobs downloaded into ~/.root/tmp
# into release dir, which is invalid cross-device link
export HOME=$PWD

git config --global user.email "ci@localhost"
git config --global user.name "CI Bot"

bosh -n add-blob ../${RESOURCE_NAME}.tar.gz resources/${RESOURCE_NAME}.tar.gz
bosh -n upload-blobs

if ! git diff-index --quiet HEAD; then
  git add -A
  git commit -m "update ${RESOURCE_NAME} blob"
fi
